﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using LX.Core;

namespace S4Store.Domain.SellerCarBrand
{
    public class Table 
    {
        public Table()
        {
            _month = DateTime.Now;
            _currentdate = DateTime.Now;
            Rows = new HashSet<Row>();
        }

        public int Id { get; set; }
        public int StoreId { get; set; }

        private DateTime _month;
        public DateTime Month
        {
            get { return _month; }
            set { _month = value; }
        }

        public virtual Store Store { get; set; }

        public virtual ICollection<Row> Rows { get; set; }



        private DateTime _currentdate;
        [NotMapped]
        public DateTime CurrentDate
        {
            get { return _currentdate; }
            set { _currentdate = value; }
        }

        [NotMapped]
        [Alias("C")]
        [Description("本月执行天数")]
        public int Days
        {
            get { return DateTime.DaysInMonth(Month.Year, Month.Month); }
        }

        [NotMapped]
        [Alias("I")]
        [Description("已执行天数")]
        public int ExcuteDays
        {
            get { return CurrentDate.Day; }
        }

        [NotMapped]
        [Description("时间进度")]
        public int? RateOfTime {
            get { return ExcuteDays * 100 / Days; }
        }
        [NotMapped]
        public int? O
        {
            get { return RateOfTime; }
        }

        [NotMapped]
        [Alias("U")]
        [Description("销量进度")]
        [Expression("Rows.Sum(R)/Rows.Sum(Z)")]
        public int? RateOfSalesVolume
        {
            get { return Calculate.Divide(Rows.Sum(t => t.R), Rows.Sum(t => t.Z)).toDefault(); }
        }
        [NotMapped]
        public int? U
        {
            get { return RateOfSalesVolume; }
        }

        [NotMapped]
        [Alias("AE")]
        [Description("销量进度")]
        public int? RateOfSalesDiff
        {
            get { return (U-O).toDefault(); }
        }

        [NotMapped]
        [Alias("AL")]
        [Description("集客进度")]
        public int? RateOfCollectCustomers
        {
            get { return Calculate.Divide(Rows.Sum(t => t.E), Rows.Sum(t => t.C)).toDefault(); }
        }
        [NotMapped]
        public int? AL
        {
            get { return RateOfCollectCustomers; }
        }

        [NotMapped]
        [Alias("AS")]
        [Description("集客时间进度差")]
        public int? RateOfCollectCustomersDiff
        {
            get { return (AL-O).toDefault(); }
        }


        [NotMapped]
        [Alias("ULT")]
        public Table LastMonthData { get; set; }

        [Expression("Rows.Sum(R)/Rows.Sum(G)")]
        [Description("平均成交率")]
        public int? AvgVr
        {
            set { _avgvr = value; }
            get
            {
                if (_avgvr != null)
                {
                    return _avgvr;
                }
                return Calculate.Divide(Rows.Sum(t => t.R), Rows.Sum(t => t.G)).toDefault();
            }
        }

        private int? _avgvr;
    }
}
